package com.ljsw.activity.ckpage;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.ljsw.activity.R;
import com.ljsw.adapter.NotCompletedInfoAdapter;
import com.ljsw.application.App;
import com.ljsw.dialog.TimeOut;
import com.ljsw.entity.pkentity.PKNotCompleteInfoEntity;
import com.ljsw.server.UpServer;
import com.ljsw.util.DialogManager;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import static com.ljsw.application.App.user;

/*****
 * 查库明细列表
 * <p>
 * 2020210.27
 * lianchao
 */
public class CkNotCompletedInfoActivity extends Activity implements View.OnClickListener {

    private static final String TAG = "CkNotCompletedInfoActivity";

    private TextView BiaoCaidNo;//  标牌明细
    private TextView cknocompleteloginuser;//  登陆人
    private TextView ck_nocompletetaskno, ck_notcompleteinfo;   ///任务编号，标题头部//任务编号

    private TextView ck_not_tvcount;// 合计
    private ListView ck_nocompletenolv;//  listview;
    private NotCompletedInfoAdapter notCompletedInfoAdapter;

    private List<PKNotCompleteInfoEntity> listinfoentitylist = new ArrayList<PKNotCompleteInfoEntity>();//内层盘库明细实体
    private Handler handler;
    private TimeOut timeout;//超时连接
    private DialogManager dmanager;//弹出框管理类
    private String param = "";// 上页面参数
    private String showAllmongye = "";//  显示总钱数
    private ImageView acnotcomblack;//  返回
    private String Taskno;//任务编号
    private App app;
    private Button hbt_ql_ruku_update;// 更新
    private ImageView cknotinfoback;
    private Button ckl_cknotinfo_update;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ck_not_completed_info);
        app = (App) this.getApplication();
        param = getIntent().getStringExtra("PKNo");//   获取数据
        Taskno = getIntent().getStringExtra("TaskNo");
        InitView();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 0://登录成功后跳转 上门服务页面
                        Log.d("login用户role------------", "");
                        notCompletedInfoAdapter = new NotCompletedInfoAdapter(listinfoentitylist, CkNotCompletedInfoActivity.this);
                        ck_nocompletenolv.setAdapter(notCompletedInfoAdapter);
                        ck_not_tvcount.setText(showAllmongye);      //  显示钱数
                        break;
                    case 1://登录超时
                        break;
                    case 2://用户不存在
                        Toast.makeText(CkNotCompletedInfoActivity.this, "加载异常", Toast.LENGTH_SHORT).show();
                        break;
                    case 3://异常加载，无法访问服务器
                        Toast.makeText(CkNotCompletedInfoActivity.this, "" + "加载异常" + App.getApp().fanhuiMsg, Toast.LENGTH_SHORT).show();
                        timeout.show();
                        timeout.setTitle("加载异常");
//                        queding();
//                        quxiao();
                        break;
                    case 4://账户长度是否正确符合要求 用户不存在
                        Toast.makeText(CkNotCompletedInfoActivity.this, "账户长度不对", Toast.LENGTH_SHORT).show();
                        break;
                    case 5://判断密码的长度密码错误
                        Toast.makeText(CkNotCompletedInfoActivity.this, "请检查密码", Toast.LENGTH_SHORT).show();
                        break;

                    case 7://用户不正确
                        Toast.makeText(CkNotCompletedInfoActivity.this, "请检查用户名", Toast.LENGTH_SHORT).show();
                        break;
                    case 8://用户不正确
                        Toast.makeText(CkNotCompletedInfoActivity.this, "无返回结果", Toast.LENGTH_SHORT).show();
                        break;
                    case 9://用户不正确
                        Toast.makeText(CkNotCompletedInfoActivity.this, "时间不对", Toast.LENGTH_SHORT).show();
                        break;

                    case 10:
                        Toast.makeText(CkNotCompletedInfoActivity.this, "标牌编号不正确！请返回上一层", Toast.LENGTH_SHORT).show();

                        break;
                    default:
                        Toast.makeText(CkNotCompletedInfoActivity.this, "请检查网络服务", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

    }

    //      param = getIntent().getStringExtra("PKNo");//   获取数据
//    Taskno = getIntent().getStringExtra("TaskNo");
//
    private void InitView() {
        ck_notcompleteinfo = (TextView) findViewById(R.id.ck_notcompleteinfo);//标题头部
        ck_notcompleteinfo.setText("未完成标牌明细");
        cknocompleteloginuser = (TextView) findViewById(R.id.cknocompleteloginuser);//  设置登陆人
        cknocompleteloginuser.setText("" + user.getName());
        ck_nocompletetaskno = (TextView) findViewById(R.id.ck_nocompletetaskno);//  查库任务编号
        ck_nocompletetaskno.setText(param);
        ck_not_tvcount = (TextView) findViewById(R.id.ck_not_tvcount);
        ck_nocompletenolv = (ListView) findViewById(R.id.ck_nocompletenolv);
        BiaoCaidNo = (TextView) findViewById(R.id.anocompletebiaopaino);
        BiaoCaidNo.setText(Taskno);
        cknotinfoback = (ImageView) findViewById(R.id.ckl_cknotinfo_ckblack_);
        cknotinfoback.setOnClickListener(this);
        ckl_cknotinfo_update = (Button) findViewById(R.id.ckl_cknotinfo_update);
        ckl_cknotinfo_update.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ckl_cknotinfo_ckblack_:
                CkNotCompletedInfoActivity.this.finish();
                break;
            case R.id.ckl_cknotinfo_update:
                LoadData();
                break;
            default:
                break;
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        LoadData();
    }

    /****
     * 获取数据
     */
    private void LoadData() {
        new Thread(new Runnable() {
            public void run() {
                if (null == param || ("").equals(param)) {
                    handler.sendEmptyMessage(10);
                } else {
                    try {
                        String netResult = UpServer.getScutcheonDetailAndSum1(param);
                        if (null == netResult || ("").equals(netResult)) {
                            handler.sendEmptyMessage(3);
                        } else {
                            listinfoentitylist.clear();
                            Log.d(TAG, "---------------------------------" + netResult);
                            listinfoentitylist = JSONObject.parseArray(netResult, PKNotCompleteInfoEntity.class);
                            Log.e(TAG, "json==============长度===============================" + listinfoentitylist.size());
                            for (int i = 0; i < listinfoentitylist.size(); i++) {
                                if (null == listinfoentitylist.get(i).getMoneyCount()) {

                                } else {
                                    showAllmongye = listinfoentitylist.get(i).getMoneyCount();
                                    listinfoentitylist.remove(listinfoentitylist.size() - 1);
                                }
                            }


                            handler.sendEmptyMessage(0);
                        }

                    } catch (TimeoutException e) {
                        handler.sendEmptyMessage(2);//查无此人信息
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                        handler.sendEmptyMessage(2);//查无此人信息
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                        handler.sendEmptyMessage(2);//不是数字
                    } catch (Exception e) {

                        e.printStackTrace();
                        Log.e("b", "异常" + e);
//                    Log.e("data报文加密失败！", e.getMessage());
                        handler.sendEmptyMessage(2);
                    }


                }
            }
        }).start();
    }

}
